package com.gy.paidao.main;

import android.graphics.Bitmap;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.VideoView;

import com.gy.paidao.BaseActivity;
import com.gy.paidao.R;
import com.gy.paidao.util.IMFileModel;
import com.gy.paidao.util.IMUploadSuccessListener;
import com.gy.paidao.util.LogUtil;
import com.gy.paidao.util.StorageUtils;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;

import java.io.File;

/**
 * @author GY
 * @date 16/4/21
 * @Description:
 */
public class BubbleDetailInfoActivity extends BaseActivity implements IMUploadSuccessListener {

    private ImageView imgBg;
    private VideoView videoView;
    private String path, flag, downPath,id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail_info);
        setViews();
    }

    @Override
    protected void initVariable() {
        flag = getIntent().getStringExtra("flag");
        path = getIntent().getStringExtra("path");
        id = getIntent().getStringExtra("id");
    }

    @Override
    protected void setTitleViews() {
    }

    @Override
    protected void setViews() {
        imgBg = (ImageView) findViewById(R.id.detail_img);
        videoView = (VideoView) findViewById(R.id.detail_videoview);
        showRequestLoading();
        if ("image".equals(flag)) {
            imgBg.setVisibility(View.VISIBLE);
            videoView.setVisibility(View.GONE);

            ImageLoader.getInstance().displayImage(path, imgBg, new ImageLoadingListener() {
                @Override
                public void onLoadingStarted(String s, View view) {
                }

                @Override
                public void onLoadingFailed(String s, View view, FailReason failReason) {
                    dismissLoading();
                }

                @Override
                public void onLoadingComplete(String s, View view, Bitmap bitmap) {
                    dismissLoading();
                }

                @Override
                public void onLoadingCancelled(String s, View view) {

                }
            });

        } else if ("video".equals(flag)) {
            imgBg.setVisibility(View.GONE);
            videoView.setVisibility(View.VISIBLE);
            videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    videoView.setVideoPath(downPath);
                    videoView.start();
                }
            });

            File file = StorageUtils.getNormaoDirectory(this);
            downPath = file.getAbsolutePath() + "/" + id + ".mp4";//根据泡泡id进行创建判断
            File fileNew = new File(downPath);
            if (fileNew.exists()) {
                Message message = new Message();
                message.obj = downPath;
                handler.sendMessage(message);
                return;
            } else {
                IMFileModel.getInstance(this).downFile(path, this, "mp4",id);
            }
        }
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            dismissLoading();
            videoView.setVideoPath(downPath);
            videoView.start();
        }
    };

    @Override
    public void messageUploadFileSuccess(String url) {
        LogUtil.e("下载成功------");
        downPath = url;
        handler.sendEmptyMessage(0);
    }

    @Override
    public void messageUploadFail(String path) {

    }
}
